package top.huhuiyu.myimages.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import top.huhuiyu.myimages.util.JsonMessage;

@ControllerAdvice
@ResponseBody
public class MyExceptionHandler {

	private static final Logger LOG = LoggerFactory.getLogger(MyExceptionHandler.class);

	@ExceptionHandler(Exception.class)
	public JsonMessage handleException(Exception ex) {
		LOG.error("程序发生错误", ex);
		if (ex instanceof NoHandlerFoundException) {
			return JsonMessage.getMessage(false, 404, "请求的资源不存在");
		}
		if (ex instanceof NullPointerException) {
			return JsonMessage.getFail("没有获取到数据");
		}
		if (ex instanceof MultipartException) {
			return JsonMessage.getFail("文件上传失败，可能超过大小限制！");
		}
		return JsonMessage.getFail("服务器忙，请稍后重试");
	}

}
